package edu.hawaii.myisern.action;

import java.io.IOException;
import javax.xml.bind.JAXBException;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationError;
import edu.hawaii.myisern.model.MyIsernModel;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * Handles the AddResearcher Action Bean.
 * 
 * @author Sonwright M. Gomez
 *
 */
public class AddResearcherActionBean extends MyIsernActionBean {
  /** The singleton MyIsernModel. */
  private MyIsernModel myIsern = MyIsernModel.getInstance();

  private String errorMessage = "";
  /** Private fields needed for researcher fields */
  @Validate(required = true)
  private String researcherName;
  private String researcherOrganization;
  private String researcherEmail;
  private String researcherPictureLink;
  private String researcherBioStatement;

  /**
   * Returns the error message, which may be empty.
   * 
   * @return The error message. 
   */
  public String getErrorMessage() {
    return this.errorMessage;
  }
  
  /**
   * Set's the error message for the user.
   * 
   * @param errorMessage containing error Message. 
   */
  public void setErrorMessage(String errorMessage) {
    this.errorMessage = errorMessage;
  }
  
  /**
   * Set's the researcher's name.
   * 
   * @param rName the name of the Researcher.
   */
  public void setResearcherName(String rName) {
    this.researcherName = rName;
  }

  /**
   * Get's the researcher's name.
   * 
   * @return ResearcherName The researcher's name.
   */
  public String getResearcherName() {
    return researcherName;
  }

  /**
   * Sets the researcher's organization.
   * 
   * @param rOrganization containing researcher's organization
   */
  public void setResearcherOrganization(String rOrganization) {
    this.researcherOrganization = rOrganization;
  }

  /**
   * Gets the researcher's organization.
   * 
   * @return the researcher's organization
   */
  public String getResearcherOrganization() {
    return researcherOrganization;
  }

  /**
   * Set's the researcher's email.
   * 
   * @param rEmail containing researcher's email address.
   */
  public void setResearcherEmail(String rEmail) {
    this.researcherEmail = rEmail;
  }

  /**
   * Get's the researcher's email address
   * 
   * @return the researcher's email address
   */
  public String getResearcherEmail() {
    return researcherEmail;
  }

  /**
   * Set's the researcher's Picture link.
   * 
   * @param rPictureLink containing the Url to the picture link.
   * 
   */
  public void setResearcherPictureLink(String rPictureLink) {
    this.researcherPictureLink = rPictureLink;
  }

  /**
   * Get's the Researcher's picture link.
   * 
   * @return the researcher's picture link.
   */
  public String getResearcherPictureLink() {
    return researcherPictureLink;
  }

  /**
   * Set's the Researcher's Bio Statement.
   * 
   * @param rBio containing the researcher's bio statement.
   */
  public void setResearcherBioStatement(String rBio) {
    this.researcherBioStatement = rBio;
  }

  /**
   * Get's the researcher's Bio Statement
   * 
   * @return the researcher's Bio Statement.
   */
  public String getResearcherBioStatement() {
    return researcherBioStatement;
  }
    
  /**
   * Add's a new researcher based on what user has entered in fields
   * 
   * @return Resolution that displays mainMenu.jsp
   * @throws IOException if IO error occurs.
   * @throws JAXBException if JAXB error occurs.
   */
  @DefaultHandler
  public Resolution submitAndSave() throws JAXBException, IOException {

    if (getResearcherName().isEmpty()) {
      ValidationError error = new SimpleError("Requires a Researcher Name");
      getContext().getValidationErrors().add("researcherName", error);
      return getContext().getSourcePageResolution();
    }
    else {
      // make sure it doesn't exist already
      if (myIsern.containsResearcher(this.researcherName)) {
        setErrorMessage("There is already a Researcher with that name found "
            + "in the database.");
        return new ForwardResolution("/addResearcher.jsp");
      }
      else {
        Researcher newR = new Researcher();

        // Add name
        newR.setName(this.researcherName.trim());

        // Add Researcher's organization
        try {
          newR.setOrganization(this.researcherOrganization.trim());
          
        }
        catch (NullPointerException e) {
          newR.setOrganization("");
        }

        // Add Researcher's email
        try {
          newR.setEmail(this.researcherEmail.trim());
        }
        catch (NullPointerException e) {
          newR.setEmail("");
        }
        // Add Researcher's Picture link
        try {
          newR.setPictureLink(this.researcherPictureLink.trim());
        }
        catch (NullPointerException e) {
          newR.setPictureLink("");
        }
        // Add Researcher's Bio Statement
        try {
          newR.setBioStatement(this.researcherBioStatement.trim());
        }
        catch (NullPointerException e) {
          newR.setBioStatement("");
        }

        myIsern.addResearcher(newR);
        myIsern.saveResearcher();
        if (myIsern.containsOrganization(this.researcherOrganization.trim())) {
          return new ForwardResolution("/mainMenu.jsp");
        }
        else {
          return new ForwardResolution ("/addOrganization.jsp");
        }
      }
    }
  }

  /**
   * Returns the user to the main menu without saving
   * 
   * @return Resolution that displays mainMenu.jsp
   */
  @DontValidate
  public Resolution cancel() {
    return new ForwardResolution("/mainMenu.jsp");
  }
}
